﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace YXShop.TemplateAction
{
    public partial class LabelParse
    {
        public string StoreType()
        {
            string mystyle = this.Label_Inserted;
            string str_IsOmit = this.GetParamValue("YX:IsOmit");//是否省略
            string str_LayoutMode = this.GetParamValue("YX:LayoutMode");//布局方式
            string str_SortMode = this.GetParamValue("YX:SortMode");//排序方式
            string str_SortField = this.GetParamValue("YX:SortField");
            string str_ShowSeries = this.GetParamValue("YX:ShowSeries");
            string str_OneRows = this.GetParamValue("YX:OneRows");
            string str_OneColumn = this.GetParamValue("YX:OneColumn");
            string str_OneMaxCharacter = this.GetParamValue("YX:OneMaxCharacter");

            string str_TowRows = this.GetParamValue("YX:TowRows");
            string str_TowColumn = this.GetParamValue("YX:TowColumn");
            string str_TowMaxCharacter = this.GetParamValue("YX:TowMaxCharacter");

            string orderSort = " [order by] sort";
            if (!string.IsNullOrEmpty(str_SortField))
            {
                orderSort = " [order by] " + str_SortField + "";
            }
            if (str_ShowSeries == "desc")
            {
                orderSort += " desc";
            }
            else
            {
                orderSort += " asc";
            }

            int oneRows = 1;
            if (ChangeHope.Common.ValidateHelper.IsNumber(str_OneRows))
            {
                oneRows = Convert.ToInt32(str_OneRows);
            }

            int oneColumn = 1;
            if (ChangeHope.Common.ValidateHelper.IsNumber(str_OneColumn))
            {
                oneColumn = Convert.ToInt32(str_OneColumn);
            }
            int onePageSize = oneRows * oneColumn;

            int TowRows = 1;
            if (ChangeHope.Common.ValidateHelper.IsNumber(str_TowRows))
            {
                TowRows = Convert.ToInt32(str_TowRows);
            }

            int TowColumn = 1;
            if (ChangeHope.Common.ValidateHelper.IsNumber(str_TowColumn))
            {
                TowColumn = Convert.ToInt32(str_TowColumn);
            }
            int TowPageSize = TowRows * TowColumn;
            StringBuilder shtml = new StringBuilder();
            if (!string.IsNullOrEmpty(mystyle))
            {
                string pattern = @"\{\#[\s\S]*?\#\}";
                string tempfinallyconent = mystyle;
                StringBuilder GetTemp = new StringBuilder();
                MatchCollection macths = Regex.Matches(tempfinallyconent, pattern);
                string Str_Tag = "";
                string temptag = "";
                foreach (Match macth in macths)
                {
                    temptag = macth.ToString();
                    int templength = temptag.ToString().IndexOf("{#") + 2;
                    int templength2 = temptag.ToString().IndexOf("#}");
                    if (temptag.ToString().Substring(templength, templength2 - templength).ToString() != null)
                    {
                        Str_Tag = temptag.ToString().Substring(templength, templength2 - templength);
                    }
                }

                YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
                YXShop.BLL.Shop.ShopCategory bll = new YXShop.BLL.Shop.ShopCategory();
                ChangeHope.DataBase.DataByPage db = bll.GetList(orderSort, onePageSize, "");
               
                if (db.DataReader != null)
                {
                    while (db.DataReader.Read())
                    {
                        StringBuilder towShtml = new StringBuilder();
                        //二级类型
                        if (Str_Tag != "")
                        {
                            ChangeHope.DataBase.DataByPage dbTow = bll.GetList(orderSort, TowPageSize, " and pid=" + db.DataReader["id"].ToString() + "");
                            if (dbTow.DataReader != null)
                            {
                                while (dbTow.DataReader.Read())
                                {
                                    string secondContent = Str_Tag;
                                    if (secondContent.IndexOf("[#SecondName]") > -1)
                                    {
                                        string SecondNameContent = dbTow.DataReader["name"].ToString();
                                        if (ChangeHope.Common.ValidateHelper.IsNumber(str_OneMaxCharacter))
                                        {
                                            string str_Omit = "";
                                            if (str_IsOmit == "true")
                                            {
                                                str_Omit = "...";
                                            }
                                            SecondNameContent = ChangeHope.Common.StringHelper.SubStringAndAppend(SecondNameContent,Convert.ToInt32(str_TowMaxCharacter), str_Omit);
                                        }
                                        secondContent = secondContent.Replace("[#SecondName]", SecondNameContent);

                                    }
                                    towShtml.Append(secondContent);
                                }
                            }
                        }
                        //一级类型
                        string strContent = mystyle;
                        if (strContent.IndexOf("[#StairName]") > -1)
                        {
                            string StairNameContent = db.DataReader["name"].ToString();
                            if (ChangeHope.Common.ValidateHelper.IsNumber(str_TowMaxCharacter))
                            {
                                string str_Omit = "";
                                if (str_IsOmit == "true")
                                {
                                    str_Omit = "...";
                                }
                                StairNameContent = ChangeHope.Common.StringHelper.SubStringAndAppend(StairNameContent,Convert.ToInt32(str_OneMaxCharacter), str_Omit);
                            }
                            strContent = strContent.Replace("[#StairName]", StairNameContent);
                        }
                        
                        if (towShtml.ToString() != null)
                        {
                            strContent = strContent.Replace(temptag, towShtml.ToString());
                        }
                        else
                        {
                            strContent = strContent.Replace(temptag, "");
                        }
                        if (strContent.IndexOf("[#URL]") > -1)
                        {
                            string url = Common.UrlStr("shoplist", "q_shoptype=" + db.DataReader["id"].ToString() + "", "shop/");
                            strContent = strContent.Replace("[#URL]", url);
                        }
                        shtml.Append(strContent);
                    }
                }
            }
            return shtml.ToString() == string.Empty ? "暂未找到店铺类型!" : shtml.ToString();
        }
    }
}
